﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using YArchitech.BIM;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.DataModel
{
	public class WallInfoOperate
	{
		public static IList<RegionArea> GetAllRegionArea()
		{
			IList<RegionArea> list = new List<RegionArea>();
			XElement xelement = XElement.Load(WallInfoOperate.pathNameRegion);
			foreach (XElement xelement2 in xelement.Elements())
			{
				list.Add(new RegionArea
				{
					RTypeId = Convert.ToInt32(xelement2.Attribute("rTypeId").Value),
					Spelling = xelement2.Attribute("spelling").Value,
					Name = xelement2.Attribute("name").Value,
					Latitude = Convert.ToDouble(xelement2.Attribute("latitude").Value),
					Longitude = Convert.ToDouble(xelement2.Attribute("longitude").Value),
					Shape3 = Convert.ToDouble(xelement2.Attribute("shape3").Value),
					Shape4 = Convert.ToDouble(xelement2.Attribute("shape4").Value)
				});
			}
			return list;
		}

		public static List<WallType> GetTermWallType(IList<Wall> listWall, WallKind wKind, int funcNum)
		{
			List<WallType> list = new List<WallType>();
			List<WallType> list2 = new List<WallType>();
			foreach (Wall wall in listWall)
			{
				if (!list2.Contains(wall.WallType))
				{
					list2.Add(wall.WallType);
				}
			}
			foreach (WallType wallType in list2)
			{
				WallType wallType2 = wallType;
				if (wallType2.Kind == wKind)
				{
					Parameter parameter = wallType2.GetParameter(BuiltInParameter.FUNCTION_PARAM);
					if (parameter.AsInteger() == funcNum)
					{
						list.Add(wallType2);
					}
				}
			}
			return list;
		}

		public static List<WallType> GetTermWallType(Document doc, WallKind wKind, int funcNum)
		{
			IList<Element> elementByCategory = YJKElementFilter.GetElementByCategory(doc, typeof(Wall), BuiltInCategory.OST_Walls);
			List<WallType> list = new List<WallType>();
			List<WallType> list2 = new List<WallType>();
			bool flag = false;
			foreach (Element element in elementByCategory)
			{
				Wall wall = (Wall)element;
				if (list2.Count < 1)
				{
					list2.Add(wall.WallType);
				}
				else
				{
					foreach (WallType wallType in list2)
					{
						if (wallType.Name == wall.WallType.Name)
						{
							flag = true;
						}
					}
					if (!flag)
					{
						list2.Add(wall.WallType);
					}
					flag = false;
				}
			}
			foreach (WallType wallType2 in list2)
			{
				WallType wallType3 = wallType2;
				if (wallType3.Kind == wKind)
				{
					Parameter parameter = wallType3.GetParameter(BuiltInParameter.FUNCTION_PARAM);
					if (parameter.AsInteger() == funcNum)
					{
						list.Add(wallType3);
					}
				}
			}
			return list;
		}

		public static List<WallType> GetDocAllWallType(Document doc)
		{
			List<WallType> list = new List<WallType>();
			IList<Element> elementByCategory = YJKElementFilter.GetElementByCategory(doc, typeof(Wall), BuiltInCategory.OST_Walls);
			foreach (Element element in elementByCategory)
			{
				Wall wall = element as Wall;
				if (!list.Contains(wall.WallType))
				{
					list.Add(wall.WallType);
				}
			}
			return list;
		}

		public static void BrightTermWall(IList<Wall> listWall, WallType wallType, Selection sel)
		{
			foreach (Wall wall in listWall)
			{
				Wall wall2 = wall;
				if (wall2.WallType.Name == wallType.Name)
				{
					RevitVersionFuncs.AddToSelection(sel, wall);
				}
			}
		}

		public static IList<HeatRatio> GetAllWallHeatRatio()
		{
			IList<HeatRatio> list = new List<HeatRatio>();
			XElement xelement = XElement.Load(WallInfoOperate.pathWallHeat);
			foreach (XElement xelement2 in xelement.Elements())
			{
				list.Add(new HeatRatio
				{
					WallName = xelement2.Attribute("wallName").Value,
					KValue = Convert.ToDouble(xelement2.Attribute("KVaule").Value)
				});
			}
			return list;
		}

		public static void ModifyWallHeatRatio(HeatRatio hr)
		{
			XElement xelement = XElement.Load(WallInfoOperate.pathWallHeat);
			foreach (XElement xelement2 in xelement.Elements())
			{
				if (xelement2.Attribute("wallName").Value == hr.WallName)
				{
					xelement2.SetAttributeValue("KVaule", hr.KValue);
				}
			}
			xelement.Save(WallInfoOperate.pathWallHeat);
		}

		public static void SetWallTypeDefaultName(Document doc, double heatRatio)
		{
			List<WallType> docAllWallType = WallInfoOperate.GetDocAllWallType(doc);
			IList<HeatRatio> allWallHeatRatio = WallInfoOperate.GetAllWallHeatRatio();
			foreach (WallType wallType in docAllWallType)
			{
				bool flag = false;
				foreach (HeatRatio heatRatio2 in allWallHeatRatio)
				{
					if (heatRatio2.WallName == wallType.Name)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					XElement xelement = XElement.Load(WallInfoOperate.pathWallHeat);
					XElement content = new XElement("HeatRatio", new object[]
					{
						new XAttribute("wallName", wallType.Name),
						new XAttribute("KVaule", heatRatio.ToString())
					});
					xelement.Add(content);
					xelement.Save(WallInfoOperate.pathWallHeat);
				}
			}
		}

		public static string GetWallThickValue()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(WallInfoOperate.wallThickPath);
			XmlNode nextSibling = xmlDocument.FirstChild.NextSibling;
			XmlNode xmlNode = nextSibling.SelectSingleNode("WallThick");
			XmlElement xmlElement = xmlNode as XmlElement;
			return xmlElement.GetAttribute("thick");
		}

		public static void SaveWallThickValue(string tValue)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(WallInfoOperate.wallThickPath);
			XmlNode nextSibling = xmlDocument.FirstChild.NextSibling;
			XmlNode xmlNode = nextSibling.SelectSingleNode("WallThick");
			XmlElement xmlElement = xmlNode as XmlElement;
			xmlElement.SetAttribute("thick", tValue);
			xmlDocument.Save(WallInfoOperate.wallThickPath);
		}

		private static string pathNameRegion = Product.InstallLocation + "\\Data\\ClimaticZonation.xml";

		private static string pathWallHeat = Product.InstallLocation + "\\Data\\WallHeatRatio.xml";

		private static string wallThickPath = Product.InstallLocation + "\\Data\\HYMarkCheck.xml";
	}
}
